let config = require('./config')

const allowCORS = function (app) {
    if(!config.isCORS){
        return
    }
    //设置跨域访问
    app.all('*', function (req, res, next) {
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
        res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
        if (req.method == 'OPTIONS') {
            res.send(200);
            /让options请求快速返回/
        } else {
            next();
        }
    });
}
const formatErrorRes = function(status=401,errMsg='请求出错'){
    errMsg = status==401 ? '用户未登录!' : errMsg;
    return {success:false,errMsg:errMsg}
}
const formatNormalRes = function(data){
    return {success:true,data:result}
}

module.exports = {
    allowCORS: allowCORS,
    formatErrorRes: formatErrorRes,
    formatNormalRes: formatNormalRes
}